package com.example.fairplay.newfpregistration.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.fairplay.newfpregistration.R;
import com.example.fairplay.newfpregistration.adapter.ItemAdapter;
import com.example.fairplay.newfpregistration.adapter.ItemGroupAdapter;
import com.example.fairplay.newfpregistration.adapter.SortAdapter;
import com.example.mylibrary.activity.BaseFragment;
import com.example.mylibrary.data.DBManager;
import com.example.mylibrary.data.Item;
import com.example.mylibrary.data.ItemGrop;
import com.example.mylibrary.data.Sort;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by fairplay on 2018/6/1.
 * 选择考场项目
 */

public class PickTestFragment extends BaseFragment {
    @Bind(R.id.lv_field)
    ListView lvField;
    @Bind(R.id.lv_project)
    ListView lvProject;
    @Bind(R.id.lv_group)
    ListView lvGroup;
    private View mView;

    private List<Sort> mSorts=new ArrayList<>();
    private List<Item> mItems=new ArrayList<>();
    private List<ItemGrop> mItemGroups=new ArrayList<>();

    private SortAdapter mSortAdapter;
    private ItemAdapter mItemAdapter;
    private ItemGroupAdapter mItemGroupAdapter;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_pick_test, container, false);
        ButterKnife.bind(this,mView);
        initObjects();
        initViews();
        initData();
        setListeners();
        return mView;
    }

    @Override
    protected void findViews() {

    }

    @Override
    protected void initObjects() {
        mSorts= DBManager.getInstance(mActivity).sortAll();
    }

    @Override
    protected void initViews() {
        mSortAdapter=new SortAdapter(mActivity,mSorts);
        mItemAdapter=new ItemAdapter(mActivity,mItems);
        mItemGroupAdapter=new ItemGroupAdapter(mActivity,mItemGroups);
        lvField.setAdapter(mSortAdapter);
        lvProject.setAdapter(mItemAdapter);
        lvGroup.setAdapter(mItemGroupAdapter);


    }

    @Override
    protected void initData() {


    }

    @Override
    protected void setListeners() {
        lvField.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                mItems.clear();
                long sortId=mSorts.get(i).getSortID();
                List<ItemGrop> itemGrops =  DBManager.getInstance(mActivity).queryItem(sortId);
                for (ItemGrop itemGrop: itemGrops){
                    List<Item> items =   DBManager.getInstance(mActivity).queryItemName(itemGrop.getItemID());
                    if(items.size()>0) {
                        mItems.addAll(items);
                    }
                }
                mItemAdapter.notifyDataSetChanged();
            }
        });

        lvProject.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                mItemGroups.clear();
                Item item=mItems.get(i);
                mItemGroups =   DBManager.getInstance(mActivity).itemGrop(item.getItemID());
                mItemGroupAdapter.notifyDataSetChanged();
            }
        });

        lvGroup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
